<template>
  <div>
    <!-- 面包屑到导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 输入框与添加按钮区域 -->
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            class="input-with-select"
            clearable
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="goodLists" style="width: 100%" border>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="700">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格" width="180">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="180">
        </el-table-column>
        <el-table-column label="创建时间" width="180">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <!-- 编辑按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.goods_id)"
            ></el-button>
            <!-- 删除用户按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeGoodsById(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 修改用户的代码 -->
    <el-dialog
      title="编辑商品"
      :visible.sync="editGoodsDialogVisible"
      width="50%"
      @close="edidGoodsDialogClosed"
    >
      <el-form
        :model="editGoodsForm"
        :rules="addGoodsFormrules"
        ref="editGoodsFormRef"
        label-width="100px"
      >
        <el-form-item label="商品 ID">
          <el-input v-model="editGoodsForm.id" disabled></el-input>
        </el-form-item>
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editGoodsForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="goods_price">
          <el-input v-model="editGoodsForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="数量" prop="goods_number">
          <el-input v-model="editGoodsForm.goods_number"></el-input>
        </el-form-item>
        <el-form-item label="重量" prop="goods_weight">
          <el-input v-model="editGoodsForm.goods_weight"></el-input>
        </el-form-item>
        <el-form-item label="介绍">
          <el-input
            type="textarea"
            autosize
            v-model="editGoodsForm.goods_introduce"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editGoodsDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editGoodsInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      goodLists: [],
      queryInfo: { query: '', pagenum: 1, pagesize: 10 },
      total: 0,
      // 添加用户对话框
      addGoodsDialogVisible: false,
      // 编辑对话框
      editGoodsDialogVisible: false,
      // 添加商品表单
      // 编辑的表单数据
      editGoodsForm: {
        id: '',
        goods_name: '',
        goods_price: '',
        goods_number: '',
        goods_weight: '',
        goods_introduce: '',
        pics: '',
        attrs: [],
      },
      // 添加商品表单校验规则
      addGoodsFormrules: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
        ],
        goods_price: [
          { required: true, message: '请输入商品价格', trigger: 'blur' },
        ],
        goods_weight: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
        ],
        goods_number: [
          { required: true, message: '请输入商品数量', trigger: 'blur' },
        ],
        goods_cat: [
          { required: true, message: '请选择商品分类', trigger: 'blur' },
        ],
      },
    }
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: {
          query: this.queryInfo.query,
          pagenum: this.queryInfo.pagenum,
          pagesize: this.queryInfo.pagesize,
        },
      })
      if (res.meta.status !== 200) {
        this.$message.error('商品列表数据获取失败！' + res.meta.msg)
      }
      this.goodLists = res.data.goods
      this.total = res.data.total
    },
    // 监听页面pagesize改变
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 监听页码值的改变
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    // 添加商品
    addGoods() {
      this.$router.push('/goods/add')
    },
    showEditDialog(id) {
      this.editGoodsDialogVisible = true
      console.log(id)
      this.editGoodsForm.id = id
    },
    // 关闭编辑对话框并且重置表单
    edidGoodsDialogClosed() {
      this.$refs.editGoodsFormRef.resetFields()
    },
    // 点击确定提交修改信息
    editGoodsInfo() {
      this.$refs.editGoodsFormRef.validate(async (valid) => {
        if (!valid) {
          return this.$message.error('填写完整的表单项')
        }
        const { data: res } = await this.$http.put(
          `goods/${this.editGoodsForm.id}`,
          this.editGoodsForm
        )
        if (res.meta.status !== 201) {
          this.$message.error('该接口有问题，等接口修改后在修改')
        }
        console.log(res)
      })
    },
    async removeGoodsById(id) {
      console.log(id)
      const confirmResult = await this.$confirm(
        '此操作将永久删除该商品信息, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$http.delete('goods/' + id)
      if (res.meta.status !== 200) {
        this.$message.error('删除商品信息失败')
      }
      console.log(res)
      this.$message.success('删除商品信息成功')
      this.getGoodsList()
    },
  },
  created() {
    this.getGoodsList()
  },
}
</script>

<style scoped>
</style>
